MAKE=make
OBJDIR=obj
CC=gcc
CFLAGS=-g -Wall -DDEBUG -DDEBUG_MACRO
LEX=flex
YACC=bison -d -y

# ************ TARGETS AND THEIR CORRECT USAGE ************
#
# COMPILATION TARGETS:
# 
# all:		compile all the files
#
# doc:		generate the document
#
# test:		run the testsuite
#
# clean: 	remove object files and generated files
# *********************************************************
SRC=grammar.tab.c ident.c lexer.c hash.c qsort.c scratchpad.c trees.c simulate.c stralloc.c program.c compiler.c icode.c

OBJ=$(addprefix $(OBJDIR)/,$(subst .c,.o,$(SRC)))

all: a.out

a.out: $(OBJDIR) $(OBJ)
	$(CC) -I$(OBJDIR) -I.. $(CFLAGS) $(OBJ) main.c

$(OBJDIR):
	mkdir -p $(OBJDIR)

$(OBJDIR)/%.o: %.c
	$(CC) $(CFLAGS) -o $@ -c $<

$(OBJDIR)/lexer.o: lexer.l grammar.tab.c

$(OBJDIR)/grammar.tab.o: grammar.tab.c opcodes.h

grammar.tab.c: grammar.y
	$(YACC) grammar.y
	-rm -f grammar.tab.*
	sed "s/y.tab.c/grammar.tab.c/g" y.tab.c  > grammar.tab.c
	-rm -f y.tab.c
	-mv y.tab.h grammar.tab.h

doc: doc/grammar.html

doc/grammar.html: grammar.y doc/grammar2html.py
	rm -f doc/*.html
	python doc/grammar2html.py

test: $(OBJDIR) $(OBJ)
	$(MAKE) -C testsuite 'CC=$(CC)' 'CFLAGS=$(CFLAGS)' 'OBJDIR=../$(OBJDIR)' 'OBJ=$(addprefix ../,$(OBJ))'

clean:
	-(cd testsuite; $(MAKE) clean)
	rm -rf $(OBJDIR)
	rm -f *.tab.c *.tab.h
	rm -f doc/*.html
	rm -f tmpfile/*
	rm -f a.out a.out.core a.out.gmon
